import pymysql
import requests
import json
school = ['PRIMARY_SCHOOL','JUNIOR_SCHOOL','SENIOR_SCHOOL']
conn = pymysql.connect('159.138.4.111','root','Wu123456789!@#','new_area')
conn2 = pymysql.connect('localhost','root','root','school')
regions = []
cursor = conn.cursor()
cursor2 = conn2.cursor()
sql = "select id from `province` "
cursor.execute(sql)
result = cursor.fetchall()
school_url = 'https://ucenter.17zuoye.com/school/areaschoolrs.api?'
for j in school:
    for i in result:
        payload = {'regions': i[0], 'level': j, '_': '1550816347802'}
        r = requests.get(school_url, params=payload)
        r.encoding = 'utf-8'
        for k in r.json()['rows']:
            if len(k)>0:
                insert_sql = "insert into `school` values ("+str(k['id'])+","+str(k['region'])+","+'"'+pymysql.escape_string(k['name'])+'"'+","+'"'+j+'"'+")"
                print(insert_sql)
                cursor2.execute(insert_sql)
conn2.commit()


